%{
AUTHOR: Felipe Arteaga

DATE:  2020-02-08
-------------------------------------------------------------------------
PROJECT: Warning
-------------------------------------------------------------------------
DESCRIPTION:

Creates table vacancies pre and post assignment by newMarket/year

Uses public (realized) data for 2018-2019, and expectations for 2020.


=========================================================================
%}

clc;clear;close all;fclose('all');
feature('DefaultCharacterSet', 'UTF-8');

pcName=char(java.lang.System.getProperty('user.name'));
if(strcmp(pcName,'felipe'))
    % PC Felipe
    myDir='/Users/felipe/Dropbox/';
    addpath(genpath([myDir,'/myMatlabFunctions/']));
    projectDir=[myDir,'git/warnings/'];
    projectDirData=[myDir,'projects/warnings/'];
end




%% 2018 and 2019

anhos=2018:2019;
for a=1:length(anhos)
    anho=anhos(a);
    anhoStr=sprintf('%i',anho);
    oferta=readtable(sprintf('/Users/felipe/Dropbox/Mineduc/dataCompilada/1. SAE/_csv/oferta_1_%i.csv',anho));
    postulantes=readtable(sprintf('/Users/felipe/Dropbox/Mineduc/dataCompilada/1. SAE/_csv/compiladoPostulantes%i.csv',anho));
    postulantes=postulantes(postulantes.participa_1==1,:);
    
    
    copago=readCsv([myDir,'/Mineduc/dataInput/copago/maximosSubvenciones/dta_porGrado/copagoPorGradoSAE',anhoStr,'.csv'],'delimiter',',');
    copago.nivel=copago.cod_nivel;
    varCopago=['cobroMax',anhoStr];
    copago.fee=copago.(varCopago);
    
    postulantes.count=ones(height(postulantes),1);
    aux=stataCollapse({'rbdAsign_1','codCursoAsign_1'},postulantes(not(isnan(postulantes.rbdAsign_1)),:),{'count'},'sum','sort',true);
    aux.rbd=aux.rbdAsign_1;
    aux.cod_curso=aux.codCursoAsign_1;
    oferta=outerjoin(oferta,aux,'keys',{'rbd','cod_curso'},'mergeKeys',true,'type','left');
    oferta.count(isnan(oferta.count))=0;
    oferta.vacantes(oferta.vacantes<oferta.count)=oferta.count(oferta.vacantes<oferta.count);
    
    oferta.seatsLibres=oferta.vacantes-oferta.count;
    assert(all(oferta.seatsLibres>=0))
    oferta.nivel=oferta.cod_nivel;
    
    oferta=outerjoin(oferta,copago,'keys',{'rbd','nivel'},'mergeKeys',true,'type','left','rightVariables','fee');
    oferta.seatsLibresFree=oferta.seatsLibres;
    oferta.seatsLibresFree(oferta.fee>0)=0;
    oferta.vacantesFree=oferta.vacantes;
    oferta.vacantesFree(oferta.fee>0)=0;
    
    preCongestion=stataCollapse({'newMarket','nivel'},oferta,{'seatsLibres','vacantes','seatsLibresFree','vacantesFree'},{'sum','sum','sum','sum'});
    preCongestion.anho=scalarForTable(anho,preCongestion);
    
    
    
    if(a>1)
        congestion=[congestion;preCongestion];
    else
        congestion=preCongestion;
    end
end
%% 2020

warning('Actualizar con copago 2020 cuando lleguen los datos')
copago=readCsv([myDir,'/Mineduc/dataInput/copago/maximosSubvenciones/dta_porGrado/copagoPorGradoSAE2019.csv'],'delimiter',',');
copago.nivel=copago.cod_nivel;
varCopago=['cobroMax2019'];
copago.fee=copago.(varCopago);

oferta=readtable('/Users/felipe/Dropbox/Mineduc/modelacion/riesgo/congestionMercados/2020/reporteEE_B0_r500oficial_2020_09_09.csv');
oferta.seatsLibres=oferta.vacPostAsignacionProm;
oferta.vacantes=oferta.preVacantes;
oferta.vacantes(oferta.vacantes<0)=0;

oferta=outerjoin(oferta,copago,'keys',{'rbd','nivel'},'mergeKeys',true,'type','left','rightVariables','fee');
oferta.seatsLibresFree=oferta.seatsLibres;
oferta.seatsLibresFree(oferta.fee>0)=0;
oferta.vacantesFree=oferta.vacantes;
oferta.vacantesFree(oferta.fee>0)=0;


preCongestion=stataCollapse({'newMarket','nivel'},oferta,{'seatsLibres','vacantes','seatsLibresFree','vacantesFree'},{'sum','sum','sum','sum'});
preCongestion.anho=scalarForTable(2020,preCongestion);

congestion=[congestion;preCongestion];

congestion.shareAvailable=congestion.seatsLibres./congestion.vacantes;
congestion.shareAvailableFree=congestion.seatsLibresFree./congestion.vacantesFree;


assert(mean(congestion.shareAvailable>1)<.1)
congestion.shareAvailable(congestion.shareAvailable>1)=1;
congestion.shareAvailableFree(congestion.shareAvailableFree>1)=1;

shareAvailable=congestion;

save([projectDirData,'/data/chile/auxiliar/spareCapacity'],'shareAvailable');

